<?php
namespace app\restaurantadmin\controller;
use think\Controller;
class Message extends Controller
{
    public function messagelist()
    {
        $uid=session('id');
        $shop= \think\Db::name('dining') ->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        $messageres= \think\Db::name('message')
            ->where('did',$shopid)
            ->alias('a')
            ->join('__USERS__ b', 'a.uid =b.id')
            ->field('a.id,a.message,a.mdate,b.username')
            ->paginate(7);

        $this->assign('messageres',$messageres);
        return $this->fetch();
    }

    //显示用户留言留言
    public function reply()
    {
        $id=input('id');

        $reply= \think\Db::name('message')
            ->where('a.id',$id)
            ->alias('a')
            ->join('__USERS__ b', 'a.uid =b.id')
            ->field('a.id,a.message,b.username')
            ->find();

        // var_dump($reply);die();
        $this->assign('reply',$reply);

        //显示管理员回复的留言信息
        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];

        $where['mid']=$id;
        $where['shopid']=$shopid;

        $adminreply= \think\Db::name('reply')
            ->where($where)
            ->order('id desc')
            ->select();
        $this->assign('adminreply',$adminreply);
        // dump($adminreply);die();
        return $this->fetch();

    }

    //回复留言
    public function addreply()
    {
        // $id=input('id');
        // echo $id;die();

        $uid=session('id');
        // echo("<script type='text/javascript'> alert($uid);</script>");
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        if(request()->isPost()){
            $data=[
                'mid'=>input('mid'),
                'mdate'=>date('Y-m-d H:m:s'),
                'message'=>input('message'),
                'shopid'=>$shopid,
                'uid'=>$uid,
            ];
            // var_dump($data) ;die;
            //添加数据前应该先验证
            $validate = \think\Loader::validate('Messageval');

            if($validate->check($data)){
                $db=\think\Db::name('reply')->insert($data);
                if($db){
                    return $this->redirect('Message/reply',['id'=>$data['mid']]);
                }
                else{
                    echo("<script type='text/javascript'> alert('添加失败！');</script>");
                }
            }else{
                // echo $validate->getError();
                $this->error($validate->getError());
                // $error=$validate->getError();
                // echo("<script type='text/javascript'> alert($error);</script>");
            }

        }
        return $this->fetch();

    }











}


?>

